package cn.daniellee.plugin;

import cn.daniellee.plugin.map.MapGenerator;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

public class QrcodeMap extends JavaPlugin implements Listener {

	public static FileConfiguration config;

	public static String prefix;

	@Override
	public void onEnable() {
		loadConfig();

		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> QrcodeMap Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
		getLogger().info(" ");

		Bukkit.getPluginCommand("qrcodemap").setExecutor(this);
	}

	private void loadConfig() {
		getLogger().info("[QrcodeMap] Loading config...");
		config = getConfig();
		prefix = "&7[&b" + config.get("prompt-prefix") + "&7] &3: &r";
		if (MapGenerator.initialize(this, config.getString("url"))) {
			getLogger().info(config.getString("reload-success"));
		} else {
			getLogger().info(config.getString("initialize-failed"));
		}
		if(!getDataFolder().exists()) getDataFolder().mkdirs();
		saveDefaultConfig();
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
		if (sender.isOp() && args.length > 0) {
			if ("reload".equalsIgnoreCase(args[0])) {
				reloadConfig();
				config = getConfig();
				prefix = "&7[&b" + config.getString("prompt-prefix") + "&7] &3: &r";
				if (MapGenerator.initialize(this, config.getString("url"))) {
					sender.sendMessage((prefix + config.getString("reload-success")).replace("&", "§"));
				} else {
					sender.sendMessage((prefix + config.getString("initialize-failed")).replace("&", "§"));
				}
			} else sendHelp(sender);
			return true;
		}
		if (sender instanceof Player) {
			MapGenerator.give((Player) sender);
		} else {
			sender.sendMessage(config.getString("console-cant-run"));
		}
		return true;

	}

	private void sendHelp(CommandSender sender) {
		sender.sendMessage(("&m&a---&m&6--------&3 : " + prefix + "&m&a--------&m&6---").replace("&", "§"));

		TextComponent reloadHelp = new TextComponent("/qm reload  " + config.get("reload-help"));
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/qm reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder((String) config.get("reload-help")).color(ChatColor.BLUE).create()));
		sender.spigot().sendMessage(reloadHelp);

	}

	@Override
	public void onDisable() {
		getLogger().info(" ");
		getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> QrcodeMap Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
		getLogger().info(" ");
	}
	
}
